/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserInterface.BarChart;

import Business.Launcher.Admin.LaunchAdmin;
import Business.Record.Record;
import Business.RecordsOperation.RecordsOperation;
  import java.awt.Font;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart {
	ChartPanel frame1;
	public  BarChart(){
		CategoryDataset dataset = getDataSet();
        JFreeChart chart = ChartFactory.createBarChart3D(
       		                 "region analysis", 
                            "regions", 
                            "number", 
                            dataset, 
                            PlotOrientation.VERTICAL, 
                            true,           
                            false,         
                            false         
                            );
        
        CategoryPlot plot=chart.getCategoryPlot();
        CategoryAxis domainAxis=plot.getDomainAxis();         
        domainAxis.setLabelFont(new Font("Monospaced 15",Font.BOLD,14));         
        domainAxis.setTickLabelFont(new Font("Monospaced 15",Font.BOLD,12));  
        ValueAxis rangeAxis=plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("Monospaced 15",Font.BOLD,15));
        chart.getLegend().setItemFont(new Font("Monospaced 15", Font.BOLD, 15));
        chart.getTitle().setFont(new Font("Monospcaed 15",Font.BOLD,20));
          
        frame1=new ChartPanel(chart,true);        
	}

	private static CategoryDataset getDataSet() {
    	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	   	Map<String, Integer> countMap = new HashMap<String, Integer>();
		List<Record> list = new RecordsOperation().getAllRecords();
		Iterator<Record> ite = list.iterator();
		while(ite.hasNext()){
			Record r = ite.next();
			if(countMap.containsKey(r.country)){
				int num = countMap.get(r.country);
				countMap.remove(r.country);
				num++;
				countMap.put(r.country, num);
			}else{
				countMap.put(r.country, 1);
			}
		}
	    Iterator<Entry<String, Integer>> iterator = countMap.entrySet().iterator();
	  	while(iterator.hasNext()){
			Entry<String, Integer> entry = iterator.next();
		   	dataset.addValue(entry.getValue(), entry.getKey(), entry.getKey());
	   	}
        return dataset;
	}
	
	public ChartPanel getChartPanel(){
		return frame1;
	}
}
